package order.service.ServiceImpl;

import io.seata.spring.annotation.GlobalTransactional;
import order.api.StockService;
import order.mapper.OrderMapper;
import order.pojo.Order;
import order.pojo.OrderDetail;
import order.pojo.Result;
import order.pojo.SecKillGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrderServiceImpl {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    StockService stockService;

    @GlobalTransactional
    public String creatOrder(Order order) {
        List<OrderDetail> details = order.getDetails();
        boolean isSuccess=true;

        for (OrderDetail o:
             details) {
            SecKillGoods secKillGoods = new SecKillGoods();
            secKillGoods.setSum(o.getSum());
            secKillGoods.setSecKillGoodId(o.getGoodsId());
//            secKillGoods.setRedisId("1_503_2022/8/25_12:00:00");
            Result<Object> deduct = stockService.deduct(secKillGoods);
            System.err.println(deduct.getMsg());
            if(!deduct.getMsg().equals("操作成功")){
                isSuccess=false;
                break;
            }
        }
        if (orderMapper.creatOrder(order) > 0&&isSuccess) {
//            int a=3/0;
            return "抢购成功，请及时付款";
        }
        return "抢购失败";
    }


}
